// SCENARIO SCRIPT

// This is the special script for your entire scenario. It contains
// special encounters and code accessable from anywhere in the scenario. it also
// contains the code that initializes important special things in the
// scenario (like shops and names and descriptions of special items).

// You can create your own states, but you should give all of them numbers greater than
// or equal to 10.

beginscenarioscript;

variables;
short bmessage,fish_tick,pcs,pcscnt,rheight,rctx,rcty,user,sp_cost;
short energy,sp_send,roost_y,heal_tick;
string custom_m;

body;

// This is the state that is called every time the scenario is loaded,
// even when a save file in the scenario is loaded. Some things that should go here:
//    Names and descriptions of special items.
//    Names and effects of custom special abilities.
beginstate LOAD_SCEN_STATE;

	force_start_day(-1);

	init_special_item(1,"Steel Key","This is a key given by Mayor Candice.  It will open up the storeroom where unneeded but still useable supplies are kept.");
	init_special_item(2,"Mysterious Amulet","This artifact was found in 'Sanctuary'.  It's a gold ball on a small chain with a red starburst on it.  It's apparently magical somehow.");
	init_special_item(3,"Boat Key","This key allows one of the brigand boats to be used.  That's about it, honestly.");
	init_special_item(17,"Spell: Transfer Energy","This spell allows for the transfer of spell energy from one character to another.");
	init_special_item(18,"Spell: Roost","This spell allows for ground below the caster to rise up, offering protection from melee attackers and the harmful effects of ranged attackers.");
	init_special_item(19,"Spell: Fracas","This spell makes all members of the party go wild with a berserker rage, having them tear through the enemy with no restraint.");
	init_special_item(20,"Spell: Esuna","This spell resets all status effects for the party and gives temporary protection from some enemies' harmful status effects.");

	init_quest(0,"Go to Sunset Creek","Sunset Creek is under siege.  The letter received from the city's Imperial Liason said to visit Sunset Creek to help them with their brigand problem.");
	init_quest(1,"Follow Voice","A voice has contacted you, telling you to follow it somewhere.  Schizophrenia has never had such rapid onset.");
	init_quest(2,"Get Underground","The brigands are apparently located underground.  The goal now is to find the pit they fall down to the northeast and to take the plunge.");
	init_quest(3,"Cross River","The main outpost of these brigands lies across the river.  To kill brigands, however, one must first get to them.");
	init_quest(4,"Kill The Immortal","The Immortal-- Gen. Rufus Rothgard-- is brainwashing Empire soldiers and ordering them to slaughter innocents.  He must be killed to preserve peace and save lives.");
	init_quest(10,"Kill Field Commander","There is a large group of brigands standing immediately outside of Sunset Creek.  While the city has held on so far, this brigand army's removal would be a source of great relief to the forces there.");

	init_special_abil(0,"Transfer Energy - ?? SP",10);
	init_special_abil(1,"Roost - 12 SP",11);
	init_special_abil(2,"Fracas - 30 SP",12);
	init_special_abil(3,"Esuna - 20 SP",13);

	break;

// This is the state that is called only once at the very beginning of 
// the scenario. Some things that should go here:
//    The stuff in shops.
//    Creating horses and boats.
beginstate START_SCEN_STATE;
	// Initialize a few shops.
	
	// Shop 0 - Fish
	add_item_to_shop(0,11,5);
	fish_tick = 0;

	// Shop 1 - Herbalist Recipes
	add_item_to_shop(1,4000,1);
	add_item_to_shop(1,4001,1);
	add_item_to_shop(1,4002,1);
	add_item_to_shop(1,4003,1);
	add_item_to_shop(1,4004,1);
	add_item_to_shop(1,4005,1);
	add_item_to_shop(1,4006,1);
	add_item_to_shop(1,4007,1);
	add_item_to_shop(1,4008,1);
	add_item_to_shop(1,4009,1);
	add_item_to_shop(1,4010,1);
	add_item_to_shop(1,4011,1);
	add_item_to_shop(1,4012,1);

	// Shop 2 - Herbalist Potions
	add_item_to_shop(2,223,8);
	add_item_to_shop(2,231,4);
	add_item_to_shop(2,240,4);
	add_item_to_shop(2,237,2);
	add_item_to_shop(2,239,1);

	// Shop 4 - Very Basic spells
	add_item_to_shop(4,3000,1);
	add_item_to_shop(4,3001,1);
	add_item_to_shop(4,3008,1);

	// Setting up caches...
	set_flag(46,0,get_ran(1,24,43));
	set_flag(47,0,get_ran(1,44,83));
	set_flag(48,0,get_ran(1,220,243));
	set_flag(49,0,get_ran(1,199,213));

	create_horse(0,0,39,28,0);

	init_special_abil(0,"Transfer Energy",10);
	init_special_abil(1,"Roost",11);
	init_special_abil(2,"Fracas",12);
	init_special_abil(3,"Esuna",13);

break;

// This state is called every tick wherever the party is in the scenario.
// You can use the set_state
beginstate START_STATE;

// Cloud of Blades Check

	pcs = 0;
	while(pcs <= 3){
		if(char_ok(pcs) == TRUE){
			while(get_spell_level(pcs,1,15) >= 1){
				change_spell_level(pcs,1,15,-1);
				inc_flag(30,(pcs + 5),1);
			}
		}
		pcs = (pcs + 1);
	}

	// No flight!
	pcs = 0;
	while(pcs <= 3){
		if(char_ok(pcs) == TRUE){
			sp_cost = get_char_status(pcs,25);
			if(sp_cost > 0){
				sp_cost = (sp_cost * -1);
				set_char_status(pcs,25,sp_cost,1,0);
			}
		}

		pcs = (pcs + 1);
	}

	// For ESUNA
	if(get_flag(90,0) > 0)
		inc_flag(90,0,-1);

	// For RIOT and ESUNA
	if(get_flag(95,0) > 0)
		inc_flag(95,0,-1);

	// For ROOST
	roost_y = 0;
	pcs = 0;
	while(pcs <= 3){
		if(get_flag(99,pcs) > 0){
			roost_y = 1;
			rctx = get_flag(97,pcs);
			rcty = get_flag(98,pcs);
			rheight = (get_height(rctx,rcty) - 1);
			set_height(rctx,rcty,rheight);
			put_effect_on_space(rctx,rcty,5,3,1);

			inc_flag(99,pcs,-1);
		}

		pcs = (pcs + 1);
	}
	if(roost_y > 0){
		force_instant_terrain_redraw();
		run_animation();
	}

	fish_tick = (fish_tick + 1);
	if(fish_tick >= 1000){
		add_item_to_shop(0,11,1);
		fish_tick = 0;
	}
	if(get_flag(17,1) > 0){
		if(heal_tick == 0){
			inc_flag(17,1,-1);
			if(get_flag(17,1) > 0)
				heal_tick = 100;
		}
		if(get_flag(17,1) > 0)
			heal_tick = (heal_tick - 1);
	}

break;

// TRANSFER ENERGY
beginstate 10;

	if(is_outdoor() == TRUE){
		print_str_color("You can't use Transfer Energy outside!",1);
		end();
	}
	user = who_used_custom_abil();

	if(get_flag(96,9) == 0){
		sp_cost = get_custom_abil_uses(pcs,0);
		change_custom_abil_uses(pcs,0,(sp_cost * -1));
		end();
	}

	if(party_size() == 1){
		print_str_color("No PCs to transfer energy to.",1);
		change_custom_abil_uses(user,0,1);
		end();
	}

	if(get_energy(user) >= 0){
		reset_dialog();
		add_dialog_str(0,"Transfer energy to which PC?",0);
		pcs = 0;
		pcscnt = 0;
		while(pcs <= 3){
			if(char_ok(pcs) == TRUE){
				if(pcs != user){
					clear_buffer();
					append_char_name(pcs);
					get_buffer_text(custom_m);
					add_dialog_choice(pcscnt,custom_m);
					pcscnt = (pcscnt + 1);
				}
			}

			pcs = (pcs + 1);
		}
		pcscnt = run_dialog(0);
		pcs = 0;
		while((pcs <= 3) && (pcscnt > 0)){
			if(char_ok(pcs) == TRUE){
				if(pcs != user){
					pcscnt = (pcscnt - 1);
				}
			}

			if(pcscnt > 0)
				pcs = (pcs + 1);
		}

		sp_cost = (33 - get_ran(get_stat(user,27),0,1));
		if(sp_cost < 28)
			sp_cost = 28;
		energy = get_energy(user);
		sp_send = (energy / 4);
		if(sp_send <= 0)
			sp_send = 1;
		energy = (energy * sp_cost);
		energy = (energy / 100);
		if(energy <= 0)
			energy = 1;
		energy = (energy * -1);

		put_effect_on_char(user,10,3,0);
		put_effect_on_char(pcs,10,2,1);
		change_char_energy(user,energy);
		change_char_energy(pcs,sp_send);
		run_animation_sound(25);

		if(is_combat() == TRUE)
			deduct_ap(5);
	}
	else{
		print_str_color("You have no energy to transfer.",1);
	}

	change_custom_abil_uses(user,0,1);

break;

// ROOST
beginstate 11;

	if(is_outdoor() == TRUE){
		print_str_color("You can't use Roost outside!",1);
		end();
	}
	user = who_used_custom_abil();

	if(get_flag(97,9) == 0){
		sp_cost = get_custom_abil_uses(pcs,1);
		change_custom_abil_uses(pcs,1,(sp_cost * -1));
		end();
	}

	sp_cost = (12 - get_ran(get_stat(user,27),0,1));
	if(sp_cost < 6)
		sp_cost = 6;

	if(get_energy(user) < sp_cost){
		print_str_color("Not enough SP.",1);
		change_custom_abil_uses(user,1,1);
		end();
	}
	if(get_flag(99,user) >= 2){
		print_str_color("Roost cannot be any higher.",1);
		change_custom_abil_uses(user,1,1);
		end();
	}
	if(current_town() == 15){
		print_str_color("Roost cannot be used here.",1);
		change_custom_abil_uses(user,1,1);
		end();
	}
	if(is_combat == FALSE){
		print_str_color("Roost can only be used in combat.",1);
		change_custom_abil_uses(user,1,1);
		end();
	}

	if(get_flag(99,user) > 0){
		rctx = get_flag(97,user);
		rcty = get_flag(98,user);
	}
	else{
		rctx = char_loc_x(user);
		rcty = char_loc_y(user);
	}
	if(get_flag(99,user) == 0)
		set_height(rctx,rcty,(get_height(rctx,rcty) + 2));
	if(get_flag(99,user) == 1)
		set_height(rctx,rcty,(get_height(rctx,rcty) + 1));
	set_flag(97,user,rctx);
	set_flag(98,user,rcty);
	set_flag(99,user,2);

	change_char_energy(user,(sp_cost * -1));
	put_effect_on_space(rctx,rcty,5,3,1);
	force_instant_terrain_redraw();
	run_animation_sound(108);
	change_custom_abil_uses(user,1,1);
	deduct_ap(5);

break;

// FRACAS
beginstate 12;

	if(is_outdoor() == TRUE){
		print_str_color("You can't use Fracas outside!",1);
		end();
	}
	user = who_used_custom_abil();

	if(get_flag(98,9) == 0){
		sp_cost = get_custom_abil_uses(pcs,2);
		change_custom_abil_uses(pcs,2,(sp_cost * -1));
		end();
	}

	sp_cost = (30 - get_ran(get_stat(user,27),0,1));
	if(sp_cost < 25)
		sp_cost = 25;

	if(get_energy(user) < sp_cost){
		print_str_color("Not enough SP.",1);
		change_custom_abil_uses(user,2,1);
		end();
	}
	if(is_combat == FALSE){
		print_str_color("Fracas can only be used in combat.",1);
		change_custom_abil_uses(user,2,1);
		end();
	}

	set_char_status(1000,1,15,0,0);
	set_char_status(1000,2,15,0,0);
	set_char_status(1000,3,15,0,0);
	set_char_status(1000,5,4,0,0);
	set_char_status(1000,9,10,1,0);
	set_char_status(1000,13,10,1,0);
	set_char_status(1000,15,15,0,0);
	set_char_status(1000,22,20,0,0);
	pcs = 0;
	while(pcs <= 3){
		if(char_ok(pcs) == TRUE){
			sp_send = (get_stat(pcs,11) + get_stat(pcs,12));
			sp_send = (sp_send + get_stat(pcs,25));
			if(sp_send > 24)
				sp_send = 24;
			set_char_status(pcs,4,(sp_send / 4),1,0);
			if(sp_send > 20)
				sp_send = 20;
			set_char_status(pcs,16,(sp_send / 2),1,0);
		}

		pcs = (pcs + 1);
	}
	change_char_energy(user,(sp_cost * -1));
	force_instant_terrain_redraw();
	play_sound(48);
	play_sound(23);
	change_custom_abil_uses(user,2,1);
	deduct_ap(5);

break;

// ESUNA
beginstate 13;

	if(is_outdoor() == TRUE){
		print_str_color("You can't use Esuna outside!",1);
		end();
	}
	user = who_used_custom_abil();

	if(get_flag(99,9) == 0){
		sp_cost = get_custom_abil_uses(pcs,3);
		change_custom_abil_uses(pcs,3,(sp_cost * -1));
		end();
	}

	sp_cost = (20 - get_ran(get_stat(user,27),0,1));
	if(sp_cost < 15)
		sp_cost = 15;

	if(get_energy(user) < sp_cost){
		print_str_color("Not enough SP.",1);
		change_custom_abil_uses(user,3,1);
		end();
	}

	pcs = 0;
	while(pcs <= 3){
		if(char_ok(pcs) == TRUE){
			energy = 0;
			while(energy <= 30){
				sp_send = get_char_status(pcs,energy);
				sp_send = (sp_send * -1);
				set_char_status(pcs,energy,sp_send,1,0);

				energy = (energy + 1);
			}
			put_boom_on_char(pcs,4,0);
		}

		pcs = (pcs + 1);
	}
	change_char_energy(user,(sp_cost * -1));
	force_instant_terrain_redraw();
	run_animation_sound(24);
	change_custom_abil_uses(user,3,1);
	deduct_ap(5);

break;

